package club.jdiy.core.sql;

import lombok.Getter;

/**
 * 数据表字段元结构信息．
 *
 * @author ziquee(子秋) QQ号:39886616 主页: http://www.jdiy.club
 * @version 20210503
 */
@Getter
public class ColumnInfo extends CommentInfo {
    public enum BaseType {
        CHAR, VARCHAR, TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT, CLOB,
        BIT,
        TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL,
        DATE, TIME, DATETIME, TIMESTAMP,

        OTHER
    }

    private final String type;//e.g:  varchar(32)
    private final BaseType baseType;
    private final boolean primaryKey;
    private final boolean nullable;
    private final String defaultValue;
    private final String refTableName;//是否外键：外键关联表
    private final String refTableColumn;//是否外键:外键关联字段
    private final String extra;//值为auto_increment表示自增。否则为空字符串

    ColumnInfo(String name, String comment,
               String baseType, String type,
               boolean primaryKey, boolean nullable, String defaultValue,
               String refTableName, String refTableColumn, String extra
    ) {
        super(name, comment);
        ColumnInfo.BaseType bt;
        try {
            bt = ColumnInfo.BaseType.valueOf(baseType.toUpperCase());
        } catch (Exception e) {
            e.printStackTrace();
            bt = BaseType.OTHER;
        }
        this.baseType = bt;
        this.type = type;
        this.primaryKey = primaryKey;
        this.nullable = nullable;
        this.defaultValue = defaultValue;
        this.refTableName = refTableName;
        this.refTableColumn = refTableColumn;
        this.extra = extra;
    }
}
